package com.api.server.controller;


import com.api.server.pojo.Equipment;
import com.api.server.pojo.RespBean;
import com.api.server.pojo.Result;
import com.api.server.service.IEquipmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shi
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Autowired
    private IEquipmentService equipmentService;

    //根据设备id查询设备信息
    @ApiOperation(value = "根据设备id查询设备信息")
    @PostMapping("/message")
    public RespBean message(String equipmentnumber){
        Equipment equipment = equipmentService.message(equipmentnumber);
        return Result.success("获取设备信息成功！",equipment);
    }


    //增加设备
    @ApiOperation(value = "增加设备")
    @PostMapping
    public Result add(@RequestBody Equipment equipment){
        if (equipmentService.message(equipment.getEquipmentnumber())!=null){
            return Result.fail("该设备编号已经被使用！");
        }
        equipmentService.add(equipment);
        return Result.success(201,"设备创建成功",equipment);
    }


    //根据id删除设备
    @ApiOperation(value = "删除设备")
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") String id){
        Equipment equipment = equipmentService.getById(id);
        if (equipment.getEquipmentnumber()==null){
            return Result.fail("不存在该设备！");
        }
        equipmentService.delete(id);
        return RespBean.success("删除设备成功！");
    }


    //根据场地查询设备
    @ApiOperation(value = "根据场地查询设备")
    @GetMapping("/{id}")
    public RespBean equByCdId(@PathVariable("id") String id){
        List<Equipment> equipment = equipmentService.equByCdId(id);
        return RespBean.success("查询成功！",equipment);
    }


    //根据id编辑设备信息
    @ApiOperation(value = "根据id编辑设备信息")
    @PutMapping("/updata")
    public RespBean updataById(@RequestBody Equipment equipment){
        int row = equipmentService.updataById(equipment);
        return RespBean.success("修改成功！",row);
    }
}
